package task.dao;

import java.util.HashMap;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import task.ConnectionDescription;

public class ConnectionDaoImpl implements ConnectionDao {
	private HashMap<String, ConnectionDescription> map = new HashMap<String, ConnectionDescription>();

	public ConnectionDaoImpl(Document doc) {
		List<Node> taskList = doc.selectNodes( "//connections/connection" );
        for (Node child : taskList) {
        	Element elem = (Element) child;
			String name = elem.attributeValue("name");
			String url = elem.attributeValue("url");
			String user = elem.attributeValue("user");
			String passwd = elem.attributeValue("passwd");
			ConnectionDescription desc = new ConnectionDescription(name, url, user, passwd);
			map.put(name, desc);
		}
	}
	@Override
	public void delete(ConnectionDescription desc) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void delete(String name) {
		throw new UnsupportedOperationException();
	}

	@Override
	public ConnectionDescription getConnectionDescription(String name) {
		return map.get(name);
	}

	@Override
	public ConnectionDescription[] getConnectionDescriptions() {
		return map.values().toArray(new ConnectionDescription[0]);
	}

	@Override
	public void save(ConnectionDescription desc) {
		throw new UnsupportedOperationException();
	}

	@Override
	public void update(ConnectionDescription desc) {
		throw new UnsupportedOperationException();
	}
}
